<?php 

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cpanel extends CI_Controller {
    
    /**
    * UI METHODS
    */

    /**
    *  @name    : index()
    *  @uses    :  Default method
    */
    public function index()
    {
        $this->login();   
    }
    
    /**
    * @name : login()
    * @uses : Cpanel login page
    */
    public function login()
    {
        //Get testimonial
        $testi = new Generalmodel();
        $testi->tables = "laci_testimonial";
        $testi->getrecordinformation();
        $hasdata = $testi->numrows;
        $testiinfo = $testi->result;
        
        $this->load->helper('text');

        $data["testimonial"] = "";
        if($hasdata){
            $data["testimonial"] = $testiinfo;
        }
        
        $data["title"] = "Ledesma Audiological Center Inc. | Login";
        $this->load->view('header', $data);
        $this->load->view('left'); 
        $this->load->view('cpanel/login');
        $this->load->view('footer');
    }
    
    /**
    * @name : logout()
    * @uses : Logout from the Control Panel
    */
    function logout()
    {
        // Store to the session
        $newdata = array(
            "id" => "",
            "username" => "",
            "group" => ""
        );  
        $this->session->set_userdata($newdata);   
        redirect(base_url() . 'index.php/cpanel/login'); 
    }
    
    /**
    * @name : dashboard()
    * @uses : Dashboard for cpanel
    */
    function dashboard()
    {
        if($this->session->userdata("group") == ADMIN){
            $data["title"] = "Ledesma Audiological Center Inc. | Dashboard";
            $this->load->view('cpanel/admin_header', $data);
            $this->load->view('cpanel/admin_left', $data);
            $this->load->view('cpanel/dashboard');
            $this->load->view('cpanel/admin_footer');        
        } else {
            redirect(base_url() . 'index.php/cpanel/login'); 
        }
    }    
    
    /**
    * AJAX METHODS
    */
    
    /**
    * @name : authenticate()
    * @uses : Authenticate the Admin username and password
    */
    function authenticate()
    {
        $cpanel = new Generalmodel();
        $condition = array(
            "adminusername" => $_POST["username"],
            "adminpassword" => $_POST["password"]
        );  
        
        $cpanel->tables = "laci_admin";
        $cpanel->conditions = $condition;
        $cpanel->getrecordinformation();
        $admininfo = $cpanel->result;
        $isexist = $cpanel->numrows;
        
        if($isexist){
            // Store to the session
            $newdata = array(
                "id" => $admininfo[0]["id"],
                "username" => $admininfo[0]["adminusername"],
                "group" => ADMIN 
            );  
            $this->session->set_userdata($newdata);      
            echo "LOGIN";   
        } else {
            echo "NOT EXIST";
        }
    }
    
    
}

/* End of file cpanel.php */
/* Location: ./application/controllers/cpanel.php */

?>
